package com.atguigu.vod.service.Impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.CustomizeException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.atguigu.vod.utils.InitVodClient.initVodClient;

/**
 * @author He Wentao
 * @since 2022/9/6 20:57
 */

@Service
public class VodServiceImpl implements VodService {

    @Override
    public R uploadVideoAly(MultipartFile file, String title, String fileName) {

        // 根据上传的文件流，获取到输入流程
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 初始化上传对象，并设置上传视频的输入流、显示的标题、原始文件名称
        UploadStreamRequest request = new UploadStreamRequest(
                ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET,
                title, fileName, inputStream);

        // 创建上传对象
        UploadVideoImpl uploader = new UploadVideoImpl();
        // 进行上传
        UploadStreamResponse response = uploader.uploadStream(request);

        // 获取到上传成功之后的视频 id
        String videoId = response.getVideoId();

        return R.ok().data("videoId", videoId);
    }


    @Override
    public R deleteVideoById(String id) {

        DefaultAcsClient client;
        DeleteVideoRequest request = new DeleteVideoRequest();
        try {
            // 获取链接对象
            client = initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            //支持传入多个视频ID，多个用逗号分隔 "VideoId1,VideoId2"
            request.setVideoIds(id);
            // 进行删除
            client.getAcsResponse(request);

        } catch (Exception e) {
            System.out.print("删除失败 = " + e.getLocalizedMessage());
            throw new CustomizeException(500, e.getLocalizedMessage());
        }
        return R.ok().message("删除成功");
    }

    @Override
    public R removeMoreAlyVideo(List<String> videoIdList) {

        DefaultAcsClient client;
        DeleteVideoRequest request = new DeleteVideoRequest();
        try {
            // 获取链接对象
            client = initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            // 使用 String 工具类，以指定符号，分割 list 集合，如: "VideoId1,VideoId2"
            request.setVideoIds(StringUtils.join(videoIdList.toArray(), ","));
            // 进行删除
            client.getAcsResponse(request);

        } catch (Exception e) {
            System.out.print("删除失败 = " + e.getLocalizedMessage());
            throw new CustomizeException(500, e.getLocalizedMessage());
        }
        return R.ok().message("删除成功");
    }
}
